package penguin.masking;

import penguin.masking.asm.CustomMethodVisitor;

/**
 * 方法的拦截器，包括转换的类，方法名及其描述
 *
 * @author wuxh
 * @version 1.0.0
 */
public class InterceptorConfig {

    private String transformClassName; //点号形式的类名
    private String method;//要代理的方法
    private String descriptor;//要代理的方法参数描述
    private Class<? extends CustomMethodVisitor> agentMethod;//代理的方法

    public InterceptorConfig(String transformClassName, String method, String descriptor, Class<? extends CustomMethodVisitor> agentMethod) {
        this.transformClassName = transformClassName;
        this.method = method;
        this.descriptor = descriptor;
        this.agentMethod = agentMethod;
    }

    public String getTransformClassName() {
        return transformClassName;
    }

    public String getMethod() {
        return method;
    }

    public String getDescriptor() {
        return descriptor;
    }

    public Class<? extends CustomMethodVisitor> getAgentMethod() {
        return agentMethod;
    }

    public CustomMethodVisitor visitor(String className, String name, String descriptor) {
        if (agentMethod == null) {
            return null;
        }
        if (!className.equals(getTransformClassName())) {
            return null;
        }
        if (!name.equals(getMethod())) {
            return null;
        }
        if (getDescriptor() != null && !getDescriptor().equals(descriptor)) {
            return null;
        }
        try {
            CustomMethodVisitor customMethodVisitor = agentMethod.newInstance();
            customMethodVisitor.config(this);
            return customMethodVisitor;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }
}
